﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using DLL.Entities;
using System.Threading;
using System.Timers;

namespace COM
{
    public partial class Form1 : Form
    {
        //Global variables for status reading purposes.
        Status MISStatus = new Status();
        Status RADStatus = new Status();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblDisable.Visible = false;
            // reading the com config
            Com com = new Com();
            TextReader reader = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Com));
                
                reader = new StreamReader("../../../DLL/XML_FILES/COM_Config.xml");
                com = (Com)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }

            //Loads the missile to 5 on startup.
            Missile misLoad = new Missile();
            XmlSerializer misReader = new XmlSerializer(typeof(Missile));
            string filePath = "../../../COM/COM_Event/COM_Event.xml";
            misLoad = (Missile)Utility.XMLRead(filePath, misReader);

            misLoad.missileNum = 5;

            Utility.WriteXML(misLoad, filePath, misReader);
        }
        private void txtMIS_Status_TextChanged(object sender, EventArgs e)
        {

        }

        #region COM_Buttons
        private void btnMIS_Fire_Click(object sender, EventArgs e)
        {
            //Button to fire missiles.
            Missile MIS_Count = new Missile();
            string file = "../../../COM/COM_Event/COM_Event.xml";
            XmlSerializer reader = new XmlSerializer(typeof(Missile));
            MIS_Count = (Missile)Utility.XMLRead(file, reader);

            if (txtMIS_Status.Text == "OFFLINE")
            {
                //Error if missile controls are offline.
                MessageBox.Show("Missile Controls are currently offline, cannot fire missile!");
            }
            else
            {
                if (lblTarget.Text == "")
                {
                    //Error if there is no target.
                    MessageBox.Show("Cannot fire without a target. Please lock on.");
                }
                else
                {
                    if (MIS_Count.missileNum == 0)
                    {
                        //Error if there is no missiles remaining.
                        MessageBox.Show("No missiles remaining");
                        MIS_Count.missilefire = "Missile has failed to fire.";
                    }
                    else
                    {
                        //If bypass all error possibilities, fire missile, decrement by one.
                        MIS_Count.missileNum -= 1;
                        MIS_Count.missilefire = "Missile has successfully fired.";
                        lblMissileFire.Visible = true;
                        pictureBox1.Visible = true;
                        misFireEvent.Enabled = true;
                        btnMIS_Fire.Enabled = false;
                    }
                    //Writes to COM_Event for missileNum update.
                    XmlSerializer serializer = new XmlSerializer(typeof(Missile));
                    TextWriter textWriter = new StreamWriter(file);
                    serializer.Serialize(textWriter, MIS_Count);
                    textWriter.Close();
                }
            }
        }

        private void btnClearTarget_Click(object sender, EventArgs e)
        {
            Target target = new Target();
            XmlSerializer reader = new XmlSerializer(typeof(Target));
            string FilePath = "../../../RAD/RAD_Event/Rad_Event.xml";
            target = (Target)Utility.XMLRead(FilePath, reader);

            target.TargetType = "";
            target.targetSpeed = "";
            target.targetAltitude = "";
            target.targetDistance = "";
            target.targetDirection = "";

            Utility.WriteXML(target, FilePath, reader);
        }
        #endregion

        #region COM_Timers
        private void StatusReader_Tick(object sender, EventArgs e)
        {
            //Reads MIS_Status and Rad_Status to see if it is online or offline and updates accordingly.
            Status CMISstatus = new Status();
            Status CRADstatus = new Status();
            XmlSerializer reader = new XmlSerializer(typeof(Status));
            CMISstatus = (Status)Utility.XMLRead("../../../MIS/XML/MIS_Status.xml", reader);
            
            //If timers are equal to each other, it is offline.
            if (MISStatus.missileStatus == CMISstatus.missileStatus)
            {
                
                txtMIS_Status.Text = "OFFLINE";
                txtMIS_Status.ForeColor = Color.Red;

                //disables Fire button to prevent COM from firing due to Missile Controls being offline.
                btnMIS_Fire.Enabled = false;
                lblDisable.Visible = true;
            }
            //Otherwise it's online.
            else
            {
                txtMIS_Status.Text = "ONLINE";
                txtMIS_Status.ForeColor = Color.Yellow;
                MISStatus.missileStatus = CMISstatus.missileStatus;
            }

            CRADstatus = (Status)Utility.XMLRead("../../../RAD/XML/RAD_Status.xml", reader);

            if (RADStatus.radarStatus == CRADstatus.radarStatus)
            {
                txtRAD_Status.Text = "OFFLINE";
                txtRAD_Status.ForeColor = Color.Red;
            }
            else
            {
                txtRAD_Status.Text = "ONLINE";
                txtRAD_Status.ForeColor = Color.Yellow;
                RADStatus.radarStatus = CRADstatus.radarStatus;
            }
        }

        private void RadarEventTimer_Tick(object sender, EventArgs e)
        {
            //Reads RAD_Event and updates randomized numbers to form.
            Target target = new Target();
            XmlSerializer reader = new XmlSerializer(typeof(Target));
            string FilePath = "../../../RAD/RAD_Event/Rad_Event.xml";
            target = (Target)Utility.XMLRead(FilePath, reader);

            lblTarget.Text = Convert.ToString(target.TargetType);
            lblSpeed.Text = Convert.ToString(target.targetSpeed);
            lblAltitude.Text = Convert.ToString(target.targetAltitude);
            lblDistance.Text = Convert.ToString(target.targetDistance);
            lblDirection.Text = Convert.ToString(target.targetDirection);
        }

        private void misFireEvent_Tick(object sender, EventArgs e)
        {
            //A bunch of widgets for when Missile has been fired.
            lblMissileFire.Visible = false;
            misFireEvent.Enabled = false;
            pictureBox1.Visible = false;
            MessageBox.Show("Target has been destroyed");
            btnMIS_Fire.Enabled = true;
        }
        #endregion
    }
}
